package com.sangmo.fts.api.receivable;

import com.sangmo.boot.framework.api.utils.ApiResponse;
import com.sangmo.boot.framework.common.security.meta.Access;
import com.sangmo.boot.framework.common.validation.meta.ValidRef;
import com.sangmo.boot.framework.data.filter.PageResult;
import com.sangmo.boot.framework.data.filter.ValueFilter;
import com.sangmo.boot.framework.data.json.ExtViewHelper;
import com.sangmo.boot.framework.plugin.dataview.data.DataViewPageSearch;
import com.sangmo.fts.basic.repo.CompanyRepo;
import com.sangmo.fts.process.receivable.interfaces.RpfContractCreation;
import com.sangmo.fts.process.receivable.model.RpfContract;
import com.sangmo.fts.process.receivable.service.RpfContractService;
import com.sangmo.fts.restructure.meta.TrainingApi;
import com.sangmo.fts.restructure.meta.TrainingProcess;
import com.sangmo.fts.training.model.TrainingHistory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;

@TrainingApi
@Controller
@RequestMapping("/rpfContract/v1")
@Access("rpfContract")
public class RpfContractApi {

    @Autowired
    private RpfContractService rpfContractService;

    @Autowired
    CompanyRepo companyRepo;

    @ValidRef(value = RpfContract.class)
    @Access(":create")
    @PostMapping(value = "/{trainingId}/create")
    public @ResponseBody
    ApiResponse<RpfContract> create(@PathVariable("trainingId") Long trainingId, @RequestBody @Valid RpfContractCreation obj) {
        obj.setTrainingId(trainingId);
        return ApiResponse.make(rpfContractService.create(obj));
    }

    @Access(":remove")
    @PostMapping("/{trainingId}/{id}/remove")
    public @ResponseBody ApiResponse remove(@PathVariable("trainingId") Long trainingId, @PathVariable("id") Long id) {
        rpfContractService.deleteById(id);
        return ApiResponse.make();
    }

    @Access(":read")
    @PostMapping("/{trainingId}/search")
    public @ResponseBody ApiResponse<PageResult<RpfContract>> search(@PathVariable("trainingId") Long trainingId, @RequestBody @Valid DataViewPageSearch<HashMap> ps) {
        ExtViewHelper.setGroup("RpfContract.search", 1);
        ps.getFilters().add(new ValueFilter("trainingId", ValueFilter.OP_EQ, trainingId));
        return ApiResponse.make(rpfContractService.search(ps));
    }

    @TrainingProcess(node = TrainingHistory.RpfProcessNodes.RPF_CONTRACT_BUYER_CONFIRM_1)
    @Access(":buyerConfirm")
    @PostMapping({"/{trainingId}/{id}/buyer/confirm"})
    @ResponseBody
    public ApiResponse buyerConfirm(@PathVariable("trainingId") Long trainingId, @PathVariable("id") Long id) {
        rpfContractService.buyerConfirm(id);
        return ApiResponse.make();
    }

    @TrainingProcess(node = TrainingHistory.RpfProcessNodes.RPF_CONTRACT_SELLER_CONFIRM_2)
    @Access(":sellerConfirm")
    @PostMapping({"/{trainingId}/{id}/seller/confirm"})
    @ResponseBody
    public ApiResponse sellerConfirm(@PathVariable("trainingId") Long trainingId, @PathVariable("id") Long id) {
        rpfContractService.sellerConfirm(id);
        return ApiResponse.make();
    }

    @Access(":waybillSelection")
    @PostMapping({"/{trainingId}/waybill/selection"})
    @ResponseBody
    public ApiResponse<RpfContract> selection(@PathVariable("trainingId") Long trainingId, @RequestBody @Valid DataViewPageSearch<HashMap> ps) {
        ps.getFilters().add(new ValueFilter("trainingId", ValueFilter.OP_EQ, trainingId));
        return ApiResponse.make(rpfContractService.waybillSelection(ps));
    }

    @Access(":loanSelection")
    @PostMapping({"/{trainingId}/loan/selection"})
    @ResponseBody
    public ApiResponse<RpfContract> loanSelection(@PathVariable("trainingId") Long trainingId, @RequestBody @Valid DataViewPageSearch<HashMap> ps) {
        ps.getFilters().add(new ValueFilter("trainingId", ValueFilter.OP_EQ, trainingId));
        return ApiResponse.make(rpfContractService.loanSelection(ps));
    }

}
